package com.bjb.vr.remote;


import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.SysConfig;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.service.SysConfigService;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.remote.ws.rabbitmq.config.IpConfiguration;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.constants.WebSocketConstants;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;
import java.util.Set;

@Slf4j
@SpringBootApplication(scanBasePackages = "com.bjb.vr")
@MapperScan({"com.bjb.vr.common.mapper"})
public class XrRemoteApplication {


    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(XrRemoteApplication.class, args);
        WebSocketServer.setApplicationContext(configurableApplicationContext);

        IpConfiguration ipConfiguration =  configurableApplicationContext.getBean(IpConfiguration.class);
        log.info("IP " + ipConfiguration.getIp() + " PORT " + ipConfiguration.getPort());

        RedisUtil redisUtil = configurableApplicationContext.getBean(RedisUtil.class);
        ResDeviceService resDeviceService = configurableApplicationContext.getBean(ResDeviceService.class);
        String ip_port = ipConfiguration.getIp() + "_" + ipConfiguration.getPort();
        String ip_port_client_key = RedisConstants.WEBSOCKET_IP_PORT_CLIENT_IDS + ip_port;
        Set<Object> redis_client_connect_ids =   redisUtil.sGet(ip_port_client_key);
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        //重启服务删除本服务连接客户端缓存
        if(redis_client_connect_ids!=null && !redis_client_connect_ids.isEmpty()){
            redis_client_connect_ids.stream().forEach(connect_id -> {
                redisUtil.del((String)connect_id);
                try{
                    String[] arrayConnectId = ((String) connect_id).split("-");
                    String groupCode = arrayConnectId[0];
                    String udid = arrayConnectId[1];
                    //更新设备为离线状态
                    if(!groupCode.equals(udid)){
                        resDeviceService.updateResDeviceOnlinieStateByUdid(udid, WebSocketConstants.WEBSOCKET_CLIENT_ONLINE_STATE_OFF);
                    }
                }catch (Exception ex){
                    log.error("更新设备离线状态失败 " + connect_id,ex);
                }
            });
        }
        SysConfigService sysConfigService = configurableApplicationContext.getBean(SysConfigService.class);
        List<SysConfig> list =   sysConfigService.list();
        int stuHeartPeriodSecond = 0;
        if(list!=null && list.size()>0){
            SysConfig sysConfig = list.get(0);
            stuHeartPeriodSecond = sysConfig.getStuOfflineTime();
        }
        WebSocketServer.startHeart(stuHeartPeriodSecond);
        log.info("XrRemoteApplication start ok ...... v4 ");
    }

}
